package com.wd.tech.adaptert;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.wd.tech.R;
import com.wd.tech.base.App;
import com.wd.tech.bean.HomePageForDetailsBean;
import com.wd.tech.bean.QueryInformationCommentListBean;
import com.wd.tech.utils.NetUtils;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author 时文豪
 * @description: 详情点击文本内容适配器
 * @date :2020/7/6 15:05
 */
public class HomePageForDetailsAdaptert extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    HomePageForDetailsBean.ResultBean bean;

    public HomePageForDetailsAdaptert(Context context, HomePageForDetailsBean.ResultBean bean) {
        this.context = context;
        this.bean = bean;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = View.inflate(context, R.layout.home_item, null);
        return new MyHome(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        StringBuffer sb = new StringBuffer();

        sb.append("<html><head><meta http-equiv='content-type' content='text/html; charset=utf-8'>");
        sb.append("<meta charset='utf-8'  content='1'></head><body style='color: black'><p></p>");
        sb.append(bean.getContent());
        sb.append("</body></html>");
        ((MyHome) holder).home_txt.getSettings().setDefaultTextEncodingName("utf-8");
        ((MyHome) holder).home_txt.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null)  ;
        ((MyHome)holder).home_txt.setBackgroundColor(0);

        Glide.with(context).load(bean.getThumbnail()).apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                .into(((MyHome) holder).iv_one);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
        ((MyHome) holder).rv_one.setLayoutManager(gridLayoutManager);
        HomeOneAdaptert homeOneAdaptert = new HomeOneAdaptert(context, bean.getInformationList());
        ((MyHome) holder).rv_one.setAdapter(homeOneAdaptert);

        SharedPreferences sp_id = App.getContext().getSharedPreferences("sp_id", Context.MODE_PRIVATE);
        int id = sp_id.getInt("id", 0);
        NetUtils.getInstance().getApis().getQueryInformationComment(id,1,10)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<QueryInformationCommentListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(QueryInformationCommentListBean queryInformationCommentListBean) {
                        List<QueryInformationCommentListBean.ResultBean> result = queryInformationCommentListBean.getResult();
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
                        ((MyHome)holder).rv_tow.setLayoutManager(linearLayoutManager);
                        QueryInformationCommentAdaptert queryInformationCommentAdaptert = new QueryInformationCommentAdaptert(context, result);
                        ((MyHome)holder).rv_tow.setAdapter(queryInformationCommentAdaptert);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public int getItemCount() {
        return bean.getPlate().size();
    }

    class MyHome extends RecyclerView.ViewHolder {

        private final ImageView iv_one;
        private final WebView home_txt;

        private final RecyclerView rv_one;
        private final RecyclerView rv_tow;


        public MyHome(@NonNull View itemView) {
            super(itemView);
            iv_one = itemView.findViewById(R.id.home_iv);
            home_txt = itemView.findViewById(R.id.home_txt);
            rv_one = itemView.findViewById(R.id.home_rv_one);
            rv_tow = itemView.findViewById(R.id.home_rv_tow);
        }
    }
}
